<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_Post extends CI_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function getAll(){
		$strSQL = "SELECT * FROM fa_post p, fa_kategori k, fa_admin a WHERE a.id_admin = p.id_admin AND p.id_kategori = k.id_kategori";
		$strQuery = $this->db->query($strSQL);
		if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $arrRow) {
                $arrStrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrStrResult;
        }else{
            return array();
        }
	}

	public function getByKategori($strId){
		$strSQL = "SELECT * FROM tb_berita b, tb_kategori k, tb_users u WHERE u.id_user = b.id_user AND b.id_kategori = k.id_kategori AND b.id_kategori = ?";
		$strQuery = $this->db->query($strSQL, $strId);
		if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $arrRow) {
                $arrStrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrStrResult;
        }else{
            return array();
        }
	}

	public function approvalPost($arrParam){
		$boolStatus = FALSE;
		$strSQL = "UPDATE tb_berita SET status = ? WHERE id_berita=?";
        if($this->db->query($strSQL, $arrParam)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}

	function insertPost($arrParam){
		$boolStatus = FALSE;
		$strSQL = "INSERT INTO fa_post(id_post,post_judul,post_isi,id_kategori,post_date,id_admin,post_gambar) VALUES(?,?,?,?,NOW(),?,?)";

        if($this->db->query($strSQL, $arrParam)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}

	function updatePost($arrParam){
		$boolStatus = FALSE;
		$strSQL = "UPDATE tb_berita SET title = ?, isi = ?, tanggal = NOW(), id_kategori = ? WHERE id_berita = ?";
        if($this->db->query($strSQL, $arrParam)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}

	function updatePostWithPicture($arrParam){
		$boolStatus = FALSE;
		$strSQL = "UPDATE tb_berita SET title = ?, isi = ?, tanggal = NOW(), id_kategori = ?, picture=? WHERE id_berita = ?";
        if($this->db->query($strSQL, $arrParam)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}

	function deletePost($strId){
		$boolStatus = FALSE;
		$strSQL = "DELETE FROM tb_berita WHERE id_berita = ?";
        if($this->db->query($strSQL, $strId)){
        	$boolStatus = TRUE;
        }else{
            $boolStatus = FALSE;
        }
		return $boolStatus;
	}

	function getMaxId(){
		$maxId = 0;
		$maxId = $this->db->query("SELECT MAX(id_berita) AS `maxId` FROM tb_berita")->row()->maxId;
		if($maxId != NULL){
			return $maxId+1;	
		}else{
			$maxId = 1;
		}
		return $maxId;
	}

	function getMyPost($strIdUser){
		$strSQL = "SELECT * FROM tb_berita b, tb_kategori k WHERE b.id_user = ? AND b.id_kategori = k.id_kategori";
		$strQuery = $this->db->query($strSQL, $strIdUser);
		if($strQuery->num_rows() > 0){
            foreach ($strQuery->result_array() as $arrRow) {
                $arrStrResult[] = $arrRow;
            }
            $strQuery->free_result();
            return $arrStrResult;
        }else{
            return array();
        }
	}

	function getPostById($strId){
		$strSQL = "SELECT * FROM tb_berita b, tb_kategori k WHERE b.id_berita = ? AND b.id_kategori = k.id_kategori";
		$strQuery = $this->db->query($strSQL,$strId);
		if($strQuery->num_rows() > 0){
            $arrResult = $strQuery->row_array();
            $strQuery->free_result();
            return $arrResult;
        }else{
            return array();
        }
	}

}
/* End of file m_post.php */
/* Location: .models/m_post.php */